jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe", //the full name of the class to create
        jsx3.gui.Block, //  extends
        null, // implements
        function (plugin) {
        	plugin.defaultTimeframeId = "24hours";
        	plugin.currentTimeframeCacheId = "SPM_CURRENT_TIMEFRAME_CACHE";
        	
        	plugin.prototype.isManager = false;
        	plugin.prototype.screenId;
        	plugin.prototype.nameSuffix = "";
        
        	plugin.selectedTimeframeSubject = "com.tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.selectedTimeframeSubject";
        	plugin.displayTimeframeSubject = "com.tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.displayTimeframeSubject";
        	
            plugin.prototype.init = function() {
                this.jsxsuper();
            }
			
			plugin.prototype.getNamespace = function() {
               return "tibco.admin";
            }
            
            plugin.prototype.initTimeframeCache = function() {
            	var server = tibco.admin.Main.getServer();
            	currentTimeframeDoc = new jsx3.xml.Document();
      			currentTimeframeDoc.createDocumentElement("Timeframe","");
      			var timeframeElement = currentTimeframeDoc.createNode(jsx3.xml.Entity.TYPEELEMENT, "TimeframeInfo", "");
      			timeframeElement.setAttribute("currentTimeframeId", plugin.defaultTimeframeId);
      			currentTimeframeDoc.appendChild(timeframeElement);
      			server.Cache.setDocument(plugin.currentTimeframeCacheId, currentTimeframeDoc);
            }
    		
    		/**
    		*  If isManager is set to true this component is the managing component and will be updating the current timeframe cache
    		* 
    		*  @param isManager a boolean which indicates if this is the managing timeframe component
    		*  @param screenId the id of the screen which this component belongs to.  This parameter should be specified if the component
    		*         is going to be hidden and displayed
    		*/
    		plugin.prototype.setUpComponent = function(nameSuffix, isManager, screenId) {
    			this.isManager = false;
    			this.screenId = null;
    			this.nameSuffix = "";
    		
    			if(isManager)
    			{
    				this.initTimeframeCache();
    				this.isManager = true;
    			}
    			tibco.admin.monitormanage.general.PageBusUtils.subscribe(plugin.selectedTimeframeSubject, this.handleTimeframeChange, this);
    			if(screenId)
    			{ 
    				this.screenId = screenId;
    				tibco.admin.monitormanage.general.PageBusUtils.subscribe(plugin.displayTimeframeSubject, this.handleDisplayTimeframe, this);
    			}
    			
    			var selectBox = this.getDescendantOfName("timeframeSelect");
				if (nameSuffix) {
					this.nameSuffix = nameSuffix;
					selectBox.setName(selectBox.getName() + nameSuffix);
				}
    			var currentTimeframeDoc = tibco.admin.Main.getServer().Cache.getDocument(plugin.currentTimeframeCacheId);
      			var timeframeId = currentTimeframeDoc.getFirstChild().getAttribute("currentTimeframeId");
	      		selectBox.setValue(timeframeId);
    		}
    		
    		plugin.prototype.handleTimeframeChange = function(subject, message, timeframeObj) {
    			var timeframeId = message.selectedTimeframe;
    			if(timeframeObj.isManager)
    			{
    				// update the current timeframe cache
    				var timeframeDoc = tibco.admin.Main.getServer().Cache.getDocument(plugin.currentTimeframeCacheId);	
    				timeframeDoc.getFirstChild().setAttribute("currentTimeframeId", timeframeId);
    			}
    			var selectBox = timeframeObj.getDescendantOfName("timeframeSelect" + timeframeObj.nameSuffix);
    			selectBox.setValue(timeframeId);
    		}
    		
    		plugin.prototype.handleDisplayTimeframe = function(subject, message, timeframeObj) {
    			if(message.screenId == timeframeObj.screenId)
    			{
    				var displayStyle = timeframeObj.getDisplay();
    				if( (!displayStyle || displayStyle == jsx3.gui.Block.DISPLAYBLOCK) && message.hideTimeframe)
    				{ timeframeObj.setDisplay(jsx3.gui.Block.DISPLAYNONE, true); }
    				else if(displayStyle == jsx3.gui.Block.DISPLAYNONE && !message.hideTimeframe)
    				{ timeframeObj.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);}
    			}
    		}
    		
    		plugin.getCurrentTimeframeId = function() {
    			var timeframeDoc = tibco.admin.Main.getServer().Cache.getDocument(plugin.currentTimeframeCacheId);	
    			return timeframeDoc.getFirstChild().getAttribute("currentTimeframeId");
    		}
    		
    		plugin.getCurrentTimeframeName = function() {
				var server = tibco.admin.Main.getServer();
    			var timeframeId = plugin.getCurrentTimeframeId();
    			var retVal;
    			if(timeframeId == "1hour")
    			{ retVal = server.getDynamicProperty("@MM@general@timeFramePastHour"); }
    			else if(timeframeId == "24hours")
    			{ retVal = server.getDynamicProperty("@MM@general@timeFramePast24Hours"); }
    			else if(timeframeId == "7days")
    			{ retVal = server.getDynamicProperty("@MM@general@timeFramePast7Days"); }
    			else if(timeframeId == "sincestarted")
    			{ retVal = server.getDynamicProperty("@MM@general@timeFrameSinceStarted"); }
    			return retVal;
    		}
            
            plugin.prototype.timeframeSelected = function(timeframe) {
	            var message = { selectedTimeframe:timeframe };
            	tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.selectedTimeframeSubject, message);
            }
            
            plugin.hideTimeframe = function(screenIdVal) {
            	var message = { screenId:screenIdVal, hideTimeframe:true };
            	tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.displayTimeframeSubject, message);
            }
            
            plugin.showTimeframe = function(screenIdVal) {
            	var message = { screenId:screenIdVal, hideTimeframe:false };
            	tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.displayTimeframeSubject, message);
            }

        });